Ext.define('CA.catalog.ObjectsTable', {
    extend: 'CA.base.Grid',
    xtype: 'CA.catalog.ObjectsTable',

    uses: [
        'Ext.ux.form.SearchField',

        'CA.base.Grid',

        'CA.catalog.models.Object',
        'CA.catalog.ObjectWindow'
    ],

    sorters: {
        property: 'name',
        direction: 'DESC'
    },

    modelType: 'CA.catalog.models.Object',
    controller: 'Catalog',
    removable: true,
    hasSearch: true,
    hasReload: true,

    buildToolbar: function(){
        return [
            {
                xtype: 'combo',
                width: 120,
                queryMode: 'local',
                store: ['Руский','Английский','Испанский']
            },
            '-',
            {
                text: 'Новый объект',
                iconCls: 'c-icon-add',
                handler: this.doCreateRecord,
                scope: this
            }
        ].concat(this.callParent());
    },

    buildColumns: function(){
        return [
            {
                text: '№',
                tdCls: 'x-grid-cell-special',
                dataIndex: 'id',
                width: 40
            },
            {
                xtype: 'booleancolumn',
                text: 'N',
                trueText: CA.trueText,
                falseText: CA.falseText,
                dataIndex: 'new',
                editor: {
                    xtype: 'checkbox',
                    fieldStyle: 'margin-left: 12px;'
                },
                resizable: false,
                style: { color: 'red' },
                width: 40
            },
            {
                xtype: 'booleancolumn',
                text: 'S',
                trueText: CA.trueText,
                falseText: CA.falseText,
                dataIndex: 'special',
                editor: {
                    xtype: 'checkbox',
                    fieldStyle: 'margin-left: 12px;'
                },
                resizable: false,
                style: { color: 'blue' },
                width: 40
            },
            {
                text: 'Наименование',
                dataIndex: 'name',
                editor: 'textfield',
                flex: 1
            },
            {
                text: 'Тип',
                dataIndex: 'type',
                editor: {
                    xtype: 'combo',
                    queryMode: 'local',
                    store: ['Вилла','Аппартамент']
                },
                width: 150
            }
        ];
    },

    doCreateRecord: function(){
        var wnd = Ext.create('CA.catalog.ObjectWindow', {
            openType: 'create',
            record: Ext.create(this.modelType)
        });

        wnd.on('apply', this.createRecord, this);
        wnd.show();
    },

    editHandler: function(grid, rowIndex, cellIndex){
        var store = this.getStore(),
            rec = store.getAt(rowIndex),
            desktop = CA.app.getDesktop(),
            id = 'catalog_object_edit_' + rec.internalId,
            wnd = desktop.getWindow(id);

        if(!wnd){
            wnd = desktop.createWindow({
                id: id,
                openType: 'edit',
                record: rec
            }, CA.catalog.ObjectWindow);
        }

        wnd.show();
    }
});
